//
//  XLDateTool.m
//  campus
//
//  Created by xl on 2019/12/13.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLDateTool.h"
#import "XLUserDefaults.h"

@implementation XLDateTool
+ (NSTimeInterval)currentTimeInMilliseconds
{
    return [XLDateTool currentTimeInMillisecondsWithServerSync:YES];
}

+ (NSTimeInterval)currentTimeInMillisecondsWithServerSync:(BOOL)flag
{
    NSTimeInterval timestamp = [[[NSDate alloc] init] timeIntervalSince1970] * 1000;
    
    if (flag){
        NSTimeInterval diff = [XLUserDefaults getServeDiffTime];
        timestamp -= diff;
    }
    timestamp = ceil(timestamp);//向上取整:12.123456 -> 13;
    
    return timestamp;
}

+ (NSString *)getDateWithTimeStamp:(NSNumber *)timeStamp formatter:(NSString *)formatter {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp.integerValue / 1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = formatter;
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)getCurrentTimeFormatter:(NSString *)formatter {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = formatter;
    return [dateFormatter stringFromDate:[NSDate date]];
}

+ (NSTimeInterval)currentTime:(NSString *)currentTime  creatTime:(NSString *)creatTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *currentDate = [formatter dateFromString:currentTime];
    NSDate *creatDate = [formatter dateFromString:creatTime];
    return [currentDate timeIntervalSinceDate:creatDate];
}

@end
